﻿Public Class frmChildDatas
    Public m_cChild As ChildInfo
    Private a, b, c, d, ee, f, g, h As New ContextMenu
    Private addr As TreeNode

    'Child Interface prototype

    Private PILL_NORMAL = 1
    Private PILL_HASCHILD = 2
    Private PILL_SELECTED = 3

    Private DIET_NORMAL = 4
    Private DIET_HASCHILD = 5
    Private DIET_SELECTED = 6

    Private BEHAVE_NORMAL = 7
    Private BEHAVE_HASCHILD = 8
    Private BEHAVE_SELECTED = 9

    Private COND_NORMAL = 10
    Private COND_HASCHILD = 11
    Private COND_SELECTED = 12

    Private BULLET = 13
    Sub AddMedicine()
        Dim sNew As String = InputBox("Enter the new medicine and dosage instructions", "Add Medicine")
        If Not String.IsNullOrEmpty(sNew) Then Me.m_cChild.Medications = sNew : extend.SaveChildren() : extend.ReloadAll() : frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Sub RemoveMedicine()
        If Me.tvInformation.SelectedNode Is Nothing Then Exit Sub
        Me.m_cChild.Remove(Me.m_cChild.Medications, Me.tvInformation.SelectedNode.Text)
        extend.SaveChildren()
        extend.ReloadAll()
        frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Sub AddDiet()
        Dim sNew As String = InputBox("Enter the diet modification", "Add Diet Modification")
        If Not String.IsNullOrEmpty(sNew) Then Me.m_cChild.DietAdjustments = sNew : extend.SaveChildren() : extend.ReloadAll() : frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Sub RemoveDiet()
        If Me.tvInformation.SelectedNode Is Nothing Then Exit Sub
        Me.m_cChild.Remove(Me.m_cChild.DietAdjustments, Me.tvInformation.SelectedNode.Text)
        extend.SaveChildren()
        extend.ReloadAll()
        frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Sub AddBehave()
        Dim sNew As String = InputBox("Enter the new behavior modification", "Add Behavior Modification")
        If Not String.IsNullOrEmpty(sNew) Then Me.m_cChild.BehaviorAdjustments = sNew : extend.SaveChildren() : extend.ReloadAll() : frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Sub RemoveBehave()
        If Me.tvInformation.SelectedNode Is Nothing Then Exit Sub
        Me.m_cChild.Remove(Me.m_cChild.BehaviorAdjustments, Me.tvInformation.SelectedNode.Text)
        extend.SaveChildren()
        extend.ReloadAll()
        frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Sub AddAllergy()
        Dim sNew As String = InputBox("Enter the new allergy or medical need and treatment instructions", "Add Allergy / Medical Need")
        If Not String.IsNullOrEmpty(sNew) Then Me.m_cChild.MedicalNeeds = sNew : frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Sub RemoveAllergy()
        If Me.tvInformation.SelectedNode Is Nothing Then Exit Sub
        Me.m_cChild.Remove(Me.m_cChild.MedicalNeeds, Me.tvInformation.SelectedNode.Text)
        extend.SaveChildren()
        extend.ReloadAll()
        frmChildDatas_Load(Nothing, Nothing)
    End Sub
    Private Sub frmChildDatas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'This program brought to you by Boomerang, some red tea, and a bowl of spicy noodles
        'Oh and maybe some wacky races if this takes much longer :(
        tvInformation.Nodes.Clear()
        Dim meds(), diet(), behave(), allergy() As String

        a.MenuItems.Clear()
        b.MenuItems.Clear()
        c.MenuItems.Clear()
        d.MenuItems.Clear()
        ee.MenuItems.Clear()
        f.MenuItems.Clear()
        g.MenuItems.Clear()
        h.MenuItems.Clear()

        a.MenuItems.Add("Add Medicine", AddressOf AddMedicine)
        b.MenuItems.Add("Add Diet Modification", AddressOf AddDiet)
        c.MenuItems.Add("Add Behaviour Modification", AddressOf AddBehave)
        d.MenuItems.Add("Add Allergy / Medical Need", AddressOf AddAllergy)

        ee.MenuItems.Add("Remove Medicine", AddressOf RemoveMedicine)
        f.MenuItems.Add("Remove Diet Modification", AddressOf RemoveDiet)
        g.MenuItems.Add("Remove Behaviour Modification", AddressOf RemoveBehave)
        h.MenuItems.Add("Remove Allergy / Medical Need", AddressOf RemoveAllergy)

        Dim m_tDietMod, m_tMedicines, m_tBehaviour, m_tAllergies, m_tConcerns As TreeNode

        meds = Me.m_cChild.Medications
        diet = Me.m_cChild.DietAdjustments
        behave = Me.m_cChild.BehaviorAdjustments
        allergy = Me.m_cChild.MedicalNeeds

        m_tMedicines = New TreeNode("Medications", IIf(meds Is Nothing, PILL_NORMAL, PILL_HASCHILD), PILL_SELECTED)
        m_tDietMod = New TreeNode("Diet Modifications", IIf(diet Is Nothing, DIET_NORMAL, DIET_HASCHILD), DIET_SELECTED)
        m_tBehaviour = New TreeNode("Behavior Modifications", IIf(behave Is Nothing, BEHAVE_NORMAL, BEHAVE_HASCHILD), BEHAVE_SELECTED)
        m_tAllergies = New TreeNode("Known Allergies / Medical Conditions", IIf(allergy Is Nothing, COND_NORMAL, COND_HASCHILD), COND_SELECTED)

        If extend.CheckPermission(ACCESS_SUPERVISOR) Then
            m_tMedicines.ContextMenu = a
            m_tDietMod.ContextMenu = b
            m_tBehaviour.ContextMenu = c
            m_tAllergies.ContextMenu = d
        End If

        Dim n As Integer = 0

        If meds IsNot Nothing Then
            For Each item As String In meds
                If String.IsNullOrEmpty(item) Then Continue For
                m_tMedicines.Nodes.Add(CStr(n), item, PILL_NORMAL)
                If extend.CheckPermission(ACCESS_SUPERVISOR) Then m_tMedicines.Nodes(CStr(n)).ContextMenu = ee
                n += 1
            Next
        End If

        If diet IsNot Nothing Then
            For Each item As String In diet
                If String.IsNullOrEmpty(item) Then Continue For
                m_tDietMod.Nodes.Add(CStr(n), item, DIET_NORMAL)
                If extend.CheckPermission(ACCESS_SUPERVISOR) Then m_tDietMod.Nodes(CStr(n)).ContextMenu = f
                n += 1
            Next
        End If

        If behave IsNot Nothing Then
            For Each item As String In behave
                If String.IsNullOrEmpty(item) Then Continue For
                m_tBehaviour.Nodes.Add(CStr(n), item, BEHAVE_NORMAL)
                If extend.CheckPermission(ACCESS_SUPERVISOR) Then m_tBehaviour.Nodes(CStr(n)).ContextMenu = g
                n += 1
            Next
        End If

        If allergy IsNot Nothing Then
            For Each item As String In allergy
                If String.IsNullOrEmpty(item) Then Continue For
                m_tAllergies.Nodes.Add(CStr(n), item, COND_NORMAL)
                If extend.CheckPermission(ACCESS_SUPERVISOR) Then m_tAllergies.Nodes(CStr(n)).ContextMenu = h
                n += 1
            Next
        End If

        m_tConcerns = New TreeNode("Concerns", BULLET, BULLET)

        m_tConcerns.Nodes.Add(m_tMedicines)
        m_tConcerns.Nodes.Add(m_tDietMod)
        m_tConcerns.Nodes.Add(m_tBehaviour)
        m_tConcerns.Nodes.Add(m_tAllergies)

        If m_cChild Is Nothing Then Exit Sub

        tvInformation.Nodes.Add(New TreeNode(String.Format("Name: {0} {1}", Me.m_cChild.FirstName, Me.m_cChild.LastName), BULLET, BULLET))
        tvInformation.Nodes.Add(New TreeNode(String.Format("Age: {0}", Me.m_cChild.Age)))
        tvInformation.Nodes.Add(New TreeNode(String.Format("Gender: {0}", Me.m_cChild.Gender)))

        addr = New TreeNode("Address", BULLET, BULLET)

        tvInformation.Nodes.Add(addr)

        addr.Nodes.Add(String.Format("Line 1: {0}", Me.m_cChild.ContactInformation.Line1))
        addr.Nodes.Add(String.Format("Line 2: {0}", Me.m_cChild.ContactInformation.Line2))
        addr.Nodes.Add(String.Format("{0}, {1} {2}", Me.m_cChild.ContactInformation.City, Me.m_cChild.ContactInformation.State, Me.m_cChild.ContactInformation.ZipCode))
        addr.Nodes.Add(String.Format("Phone number: {0}", Me.m_cChild.ContactInformation.PhoneNumber))

        PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
        PictureBox1.BackgroundImage = Me.m_cChild.Photo

        tvInformation.Nodes.Add(m_tConcerns)

        Me.Icon = Icon.FromHandle(My.Resources.information.GetHicon)

        If extend.CheckPermission(ACCESS_SUPERVISOR) Then cmdExpel.Visible = True
    End Sub
    Private Sub tvInformation_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvInformation.BeforeExpand
        If e.Node Is addr Then
            If Not extend.CheckPermission(ACCESS_SUPERVISOR) Then
                e.Cancel = True
            End If
        End If
    End Sub
    Private Sub cmdExpel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExpel.Click
        If MsgBox("Are you sure you want to expel this child from the system?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical, "Confirmation") = MsgBoxResult.Yes Then
            Try
                IO.File.Delete(Me.m_cChild.SaveFile)
            Catch ex As Exception
                Debug.WriteLine(ex.Message, "FFFFF")
            End Try
            extend.ReloadAll()
        End If
    End Sub
End Class